package com.youkeda.test.threadpool;

/**
 * @author xshuai
 */
public class TicketMachine implements Runnable {
    //名称
    private String name;
    //售票锁
    private static Object ob = new Object();
    private static int count = 5;

    public TicketMachine(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (count > 0) {
            //锁，防止并发售票错乱
            synchronized (ob) {
                //售票
                if (count > 0) {
                    System.out.println(name + "卖出了第" + count + "张票");
                    count--;
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("票已经卖完了");
    }
}
